package org.jeecg.modules.app.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.order.service.AppMemberOrderService;
import org.jeecg.modules.app.order.vo.AppMemberOrderEnt;
import org.jeecg.modules.power.group.service.IYlGroupService;
import org.jeecg.modules.power.order.entity.YlMemberOrder;
import org.jeecg.modules.power.order.service.IYlMemberOrderService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.order.controller
 * @Author: mi
 * @CreateTime: 2024-07-17 09:31
 * @Description:
 */
@CrossOrigin
@Api(tags = "APP运营端会员充值订单相关接口")
@RestController
@RequestMapping("/api/appMemberOrder")
public class AppMemberOrderController {


    @Autowired
    private IYlMemberOrderService ylMemberOrderService;//会员订单
    @Autowired
    private AppMemberOrderService appMemberOrderService;//app会员订单 业务表

    //小组
    @Autowired
    private IYlGroupService ylGroupService;

    //AppMemberOrderEnt getAppMemberOrderEntCycle(Integer type)
    @ApiLogin
    @ApiOperation("APP运营端-获取会员订单（今日、昨日、本周、本月）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "时间类型（1今日、2昨日、3本周、4本月）", required = true),
    })
    @GetMapping("/getAppMemberOrderEntCycle")
    public Result<AppMemberOrderEnt> getAppMemberOrderEntCycle(HttpServletRequest request,Integer type){
        SysUser user=new ObjectMapper().convertValue(request.getAttribute("sysUser"),SysUser.class);//转对象 用户登陆对象
        if (oConvertUtils.isEmpty(type))return Result.error_599("时间类型不可为空");
        if (type==1 ||type==2 ||type==3 ||type==4){
            AppMemberOrderEnt appMemberOrderEnt=   appMemberOrderService.getAppMemberOrderEntCycle(user,type);
            return Result.OK(appMemberOrderEnt);
        }
        return Result.error_599("时间类型错误");
    }


    //会员充值订单折线图
    @ApiLogin
    @ApiOperation("APP运营端-获取会员订单折线图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cycleTime", value = "时间周期近 （1 、30天 2、 6个月)", required = true),
    })
    @GetMapping("/getAppMemberOrderLineChart")
    public Result<?>getAppMemberOrderLineChart(HttpServletRequest request,Integer cycleTime){
        SysUser user=new ObjectMapper().convertValue(request.getAttribute("sysUser"),SysUser.class);//转对象 用户登陆对象
        if (oConvertUtils.isEmpty(cycleTime))return Result.error_599("时间周期不可为空");
        if (cycleTime==1 || cycleTime==2){
            return appMemberOrderService.getAppMemberOrderLineChart(user,cycleTime);
        }
        return Result.error_599("时间周期错误");


    }



    //会员充值列表  getAppMemberOrderPageList
    @ApiLogin
    @ApiOperation("APP运营端-获取会员订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo" ,value = "pageNo",required = true),
            @ApiImplicitParam(name = "pageSize" ,value = "pageSize",required = true),
    })
    @GetMapping("/getAppMemberOrderPageList")
    public Result< List<YlMemberOrder>>getAppMemberOrderPageList(HttpServletRequest request, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        SysUser user=new ObjectMapper().convertValue(request.getAttribute("sysUser"),SysUser.class);//转对象 用户登陆对象
        List<String> stringList=new LinkedList<>();
        //判断是不是组长 是否组长1是2否
        if (user.getIsLeaders().equals("1")){
            stringList=ylGroupService.getGroupUserById(user.getGroupIds());
        }else {
            //根据群组id 获取数据id
            stringList.add(user.getId());
        }
        QueryWrapper<YlMemberOrder> queryWrapper =new QueryWrapper<YlMemberOrder>();
        queryWrapper.eq("pay_status",1);
        queryWrapper.orderByDesc("pay_time");
        if (stringList.size()>0){//说明是业务登陆
            queryWrapper.in("bd_id", stringList);
        }
        Page<YlMemberOrder> page = new Page<YlMemberOrder>(pageNo, pageSize);
        IPage<YlMemberOrder> pageList = ylMemberOrderService.page(page, queryWrapper);
        Result<List<YlMemberOrder>> result = new Result<List<YlMemberOrder>>();
        result.setCode(200);
        result.setResult(pageList.getRecords());
        return result;
    }




















}
